package com.oig.sys.auth.controller;


import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.auth.dto.DepartmentDto;
import com.oig.sys.auth.service.ITbDepartmentService;
import com.oig.sys.auth.vo.DepartmentVo;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "2-部门管理")
@RestController
@RequestMapping("/depart")
@Slf4j
public class TbDepartmentController {

    private final ITbDepartmentService departmentService;

    @Autowired
    public TbDepartmentController(ITbDepartmentService departmentService){
        this.departmentService = departmentService;
    }

    @PreAuthorize("@pms.hasPermission('/depart/addDepartment')")
    @SysLog(value="添加部门",apiUrl="/depart/addDepartment")
    @Operation(summary = "21-添加部门,添加部门,考虑是否同时选部门人员")
    @PostMapping(value = "/addDepartment")
    public DepartmentVo addDepartment(@RequestBody @Validated(GroupAdd.class) DepartmentDto departmentDto){
        log.info("add department, {}", departmentDto);
        if (departmentDto.getTenantId()==0){
            throw new BadRequestException("请选择租户，所属租户id不能为0") ;
        }
        return this.departmentService.addDepartment(departmentDto);
    }


    @PreAuthorize("@pms.hasPermission('/depart/updateDepartment')")
    @SysLog(value="修改部门",apiUrl="/depart/updateDepartment")
    @Operation(summary = "22-修改部门,修改部门，可以同时对部门人员批量增减")
    @PostMapping(value = "/updateDepartment")
    public DepartmentVo updateDepartment(@RequestBody @Validated(GroupEdit.class) DepartmentDto departmentDto){
        log.info("add department, {}", departmentDto);
        return this.departmentService.updateDepartment(departmentDto);
    }


    @PreAuthorize("@pms.hasPermission('/depart/delDepartment')")
    @SysLog(value="物理删除部门",apiUrl="/depart/delDepartment")
    @Operation(summary = "24-物理删除部门,物理删除部门及部门员工关系")
    @PostMapping(value = "/delDepartment/{departmentId}")
    public SysResponse<Integer> delDepartment(@PathVariable("departmentId") Integer departmentId){
        log.info("del department id={}", departmentId) ;
        this.departmentService.delDepartment(departmentId);
        return SysResponse.ok(departmentId,"物理删除成功");
    }


    @PreAuthorize("@pms.hasPermission('/depart/cancelDepartment')")
    @SysLog(value="逻辑删除部门",apiUrl="/depart/cancelDepartment")
    @Operation(summary = "23-逻辑删除部门,逻辑删除部门")
    @PostMapping(value = "/cancelDepartment/{departmentId}")
    public SysResponse<Integer> cancelDepartment(@PathVariable("departmentId") Integer departmentId){
        log.info("cancel department id={}", departmentId) ;
        this.departmentService.cancelDepartment(departmentId);
        return SysResponse.ok(departmentId,"物理删除成功");
    }

    @PreAuthorize("@pms.hasPermission('/depart/revertDepartment')")
    @SysLog(value="恢复部门",apiUrl="/depart/revertDepartment")
    @Operation(summary = "恢复部门")
    @PostMapping(value = "/revertDepartment/{departmentId}")
    public SysResponse<Integer> revertDepartment(@PathVariable("departmentId") Integer departmentId){
        log.info("revert department Id:{}",departmentId);
        this.departmentService.revertDepartment(departmentId);
        return SysResponse.ok(departmentId,"恢复成功");
    }

    //@PreAuthorize("@pms.hasPermission('/depart/findDepartTree')")
    @Operation(summary = "20-根据公司id获取部门树,租户tenantId=公司id")
    @PostMapping(value = "/findDepartTree/{tenantId}")
    public List<DepartmentVo> findDepartTree(@PathVariable("tenantId") Integer tenantId){
        log.info("findDepartTree 租户id {}", tenantId);
        if (tenantId==0){
            tenantId = SecurityUtil.getUserForIfNullBuild().getTenantId();
        }
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            if (!tenantId.equals(SecurityUtil.getDefineUserMain().getTenantId())){
                throw new BadRequestException("orgzId参数错误，不是租户或无租户权限");
            }
        }
        return this.departmentService.findDepartTree(tenantId);
    }



    //@PreAuthorize("@pms.hasPermission('/depart/listDepartment')")
    @Operation(summary = "25-查询部门,根据租户查询，或用于人员修改选部门时的下拉筛选，默认返回前10条记录，前端组树，查全部，可以传一个大的pageSize")
    @PostMapping(value = "/listDepartment")
    public List<DepartmentVo> listDepartment(@RequestBody SysRequest<DepartmentDto> departRequest){
        log.info("list department, {}", departRequest);
        if (departRequest.getParamDto()==null){
            DepartmentDto departmentDto = new DepartmentDto();
            departmentDto.setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
            departRequest.setParamDto(departmentDto);
        } else if (departRequest.getParamDto().getTenantId()==null||departRequest.getParamDto().getTenantId()==0){
            departRequest.getParamDto().setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
        }
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            if (!departRequest.getParamDto().getTenantId().equals(SecurityUtil.getDefineUserMain().getTenantId())){
                throw new BadRequestException("orgzId参数错误，不是租户或无租户权限");
            }
        }
        return this.departmentService.listDepartment(departRequest);
    }


    @Operation(summary = "26-查看部门人员,查看详情，包含子表信息")
    @PostMapping(value = "/info/{departmentId}")
    public DepartmentVo info(@PathVariable("departmentId") Integer departmentId){
        log.info("info department id={}", departmentId) ;
        return this.departmentService.getDepartment(departmentId);
    }




}
